package com.example.q_kang.hhh.login;

import com.example.q_kang.hhh.api.Api;
import com.example.q_kang.hhh.api.JsonCallBack;
import com.hjq.toast.ToastUtils;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.sskj.base.BasePresenter;
import com.example.q_kang.hhh.login.ForgetPwdActivity;
import com.sskj.base.api.ApiObserver;
import com.sskj.base.api.Constant;
import com.sskj.base.base.HttpData;
import com.sskj.base.bean.RegisterBean;
import com.sskj.base.utils.RxUtils;


/**
 * @author Hey
 * Create at  2019/04/11
 */
class ForgetPwdPresenter extends BasePresenter<ForgetPwdActivity> {

    public void sendCode(String mobile) {
        httpService.getCode(mobile)
                .tag(this)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtils.show(httpData.getMsg());
                        if (httpData.getStatus() == Constant.OK) {
                            mView.sendCodeSuccess();
                        }

                    }
                });
    }

    public void resetPwd(String mobile, String code, String pwd) {
        httpService.forgetPwd(mobile, code, pwd)
                .tag(this)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData body = response.body();
                        if (body.getStatus() == Constant.OK) {
                            mView.finish();
                        }
                    }

                    @Override
                    public void onStart(Request<HttpData, ? extends Request> request) {
                        super.onStart(request);
                        mView.showLoading();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        mView.hideLoading();
                    }
                });
//        Api.getInstance().resetPassword(mobile, code, pwd, pwd)
//                .compose(RxUtils.transform())
//                .subscribe(new ApiObserver<String>(this, true) {
//                    @Override
//                    public void onSuccess(String data) {
//                        mView.resetSuccess();
//                    }
//                });
    }

    public void getIsNeedSMS() {
        httpService.getIsNeedSMS()
                .tag(this)
                .execute(new JsonCallBack<HttpData<RegisterBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<RegisterBean>> response) {
                        HttpData<RegisterBean> body = response.body();
                        if (body.getStatus() == Constant.OK) {
                            mView.isShowSMS(body.getData());
                        }
                    }
                });
    }

}
